package com.my.zidingyikongjian.view.five;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 项目名称：drawText详解
 * 创建人：mwb
 * 创建时间：2018/7/17 9:35
 */

public class MyView extends View {


    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int baseLineX = 0;
        int baseLineY = 200;

        Paint paint = new Paint();
        paint.setColor(Color.RED);
//        canvas.drawLine(baseLineX, baseLineY, 30000, baseLineY, paint);  // 画基线

        // 写文字
        paint.setColor(Color.GREEN);
        paint.setTextSize(120); // 像素
        canvas.drawText("harvic's blog", baseLineX, baseLineY, paint);

        // 计算各个线所在位置
        // 字体度量
        Paint.FontMetrics fm = paint.getFontMetrics();
        float ascent = baseLineY + fm.ascent;
        float descent = baseLineY + fm.descent;
        float top = baseLineY + fm.top;
        float bottom = baseLineY + fm.bottom;

//        Log.i("mwb", "ascent:" + fm.ascent);
//        Log.i("mwb", "descent:" + fm.descent);
//        Log.i("mwb", "top:" + fm.top);
//        Log.i("mwb", "bottom:" + fm.bottom);

        // 画基线
        paint.setColor(Color.RED);
        canvas.drawLine(baseLineX, baseLineY, 3000, baseLineY, paint);

        // 画top
        paint.setColor(Color.BLUE);
        canvas.drawLine(baseLineX, top, 3000, top, paint);

        // 画ascent
        paint.setColor(Color.GREEN);
        canvas.drawLine(baseLineX, ascent, 3000, ascent, paint);

        // 画descent
        paint.setColor(Color.BLACK);
        canvas.drawLine(baseLineX, descent, 3000, descent, paint);

        // 画bottom
        paint.setColor(Color.RED);
        canvas.drawLine(baseLineX, bottom, 3000, bottom, paint);
    }
}
